"use strict";
/*
* wgeLookupFilter.js
*
*  Created on: 2018-3-12
*      Author: Wang Yang
*        Blog: http://blog.wysaid.org
*        Mail: admin@wysaid.org
*/

WGE.FilterLookup = WGE.Class(WGE.FilterInterface, {

    fragmentShader : "" +
    "precision mediump float;\n" +
    "varying vec2 textureCoordinate;\n" +
    "uniform sampler2D inputImageTexture;\n" +
    "uniform sampler2D lookupTexture;\n" +
    "\n" +
    "const float stepDis = 1.0 / 8.0;\n" +
    "const float perPixel = 1.0 / 512.0;\n" +
    "const float halfPixel = 0.5 / 512.0;\n" +
    "\n" +
    "void main()\n" +
    "{\n" +
    "    vec4 color = texture2D(inputImageTexture, textureCoordinate);\n" +
    "\n" +
    "    float blue = color.b * 63.0;\n" +
    "    vec2 coord1;\n" +
    "    coord1.y = floor(blue / 8.0);\n" +
    "    coord1.x = floor(blue) - (coord1.y * 8.0);\n" +
    "    \n" +
    "    coord1 = coord1 * stepDis + halfPixel + (stepDis - perPixel) * color.xy;\n" +
    "    gl_FragColor.rgb = texture2D(lookupTexture, coord1).rgb;\n" +
    "    gl_FragColor.a = color.a;\n" +
    "}",

    lookupTexture : null,

    onInit : function () {
        WGE.FilterInterface.onInit.call(this);
        if(this.program) {
            this.program.bind();
            this.program.sendUniform1i("lookupTexture", 1);
        }
    },

    isValid : function () {
        return this.program && this.lookupTexture;
    },

    render : function (handler, srcTex, buffer, fbo) {
        var gl = this.context;
        gl.activeTexture(gl.TEXTURE1);
        gl.bindTexture(gl.TEXTURE_2D, this.lookupTexture);
        WGE.FilterInterface.render.apply(this, arguments);
    }


});